/*
 * @Description: 文件内容描述
 * @Author: 李志杰
 * @Date: 2020-02-02 12:24:27
 * @LastEditTime: 2020-03-16 23:18:03
 * @LastEditors: huacong
 */
import _ from 'lodash';
import * as apis from '@/services/SystemMgr';
import { setAuthority } from '@/utils/authority';

const PermissionsModel = {
  namespace: 'Permissions',
  state: {
    menus: [],
    tabs: [],
    buttons: [],
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(apis.getCurrentUserPermissionsUsingGET, {});
      const menuPerCodes = [];
      const ButtonPerCodes = [];
      const tabCodes = [];
      if (response && response.data) {
        _.forEach(response.data.menus, element => {
          menuPerCodes.push(element.perCode);
        });
        _.forEach(response.data.buttons, element => {
          ButtonPerCodes.push(element.perCode);
        });
        _.forEach(response.data.tabs, element => {
          tabCodes.push(element.perCode);
        });
        yield setAuthority(menuPerCodes);
        yield put({
          type: 'save',
          payload: { menus: menuPerCodes, buttons: ButtonPerCodes, tabs: tabCodes },
        });
        if (payload && payload.callback) {
          payload.callback(response.data);
        }
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default PermissionsModel;
